package com.tust.service;

import com.tust.bean.Course;
import com.tust.bean.TeachCourse;
import com.tust.bean.Teacher;
import com.tust.dao.CourseDao;
import com.tust.dao.TeachCourseDao;
import com.tust.dao.TeacherDao;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class TeacherCourseServiceImpl implements BaseService{

    public void service(Connection connection) {

        Scanner scanner = new Scanner(System.in);
        String type;

        List<TeachCourse> list;

        while (true) {
            System.out.println("请输入教师任课管理操作命令：add->添加，edit->编辑,del->删除,Query->查询，QUIT->退出");
            TeachCourseDao teachCourseDao = new TeachCourseDao(connection);
            type = scanner.next();

            if (type.equalsIgnoreCase("add")) {
                System.out.println("------- 教师信息表 -------");
                List<Teacher> list_teacher = new TeacherDao(connection).query();
                new TeacherServiceImpl().print(list_teacher);

                System.out.println("--------- 课程信息表 ---------");
                List<Course> list_course = new CourseDao(connection).query();
                new CourseServiceImpl().print(list_course);

                System.out.println("请依次单独输入要添加授课信息的教师工号、课程号：");
                TeachCourse teachCourse = new TeachCourse();
                teachCourse.setTid(scanner.nextInt());
                teachCourse.setCid(scanner.next());
                if (teachCourseDao.insert(teachCourse)) {
                    System.out.println("添加成功");
                    list = teachCourseDao.query();
                    print(list);
                } else {
                    System.out.println("添加失败");
                }
            } else if (type.equalsIgnoreCase("del")) {
                // 打印授课记录表
                list = teachCourseDao.query();
                print(list);

                System.out.println("删除指定教师的授课记录请输入教师工号，删除所有教师的所有授课记录请输入alls");
                TeachCourse teachCourse = new TeachCourse();
                type = scanner.next();
                // 如果是alls 删除所有教师的所有授课记录
                if(type.equalsIgnoreCase("alls")) {
                    if (teachCourseDao.delete()) {
                        System.out.println("所有教师的所有授课记录删除成功！");
                    } else {
                        System.out.println("所有教师的所有授课记录删除失败！");
                    }
                    // 否则删除指定教师的记录
                } else {
                    teachCourse.setTid(Integer.parseInt(type));
                    System.out.println("删除当前教师的指定授课记录请输入课程号，删除当前教师的所有授课记录请输入all");
                    type = scanner.next();

                    // 如果是all 删除指定教师的所有记录
                    if (type.equalsIgnoreCase("all")) {

                        if (teachCourseDao.delete(teachCourse.getTid())) {
                            System.out.println("当前教师的所有授课记录删除成功！");
                            list = teachCourseDao.query();
                            print(list);
                        } else {
                            System.out.println("当前教师的所有授课记录删除失败！");
                        }
                        // 否则删除当前教师的指定课程的授课记录
                    } else {
                        if (teachCourseDao.delete(type)) {
                            System.out.println("当前教师的" + type + "课程授课信息删除成功！");
                            list = teachCourseDao.query();
                            print(list);
                        } else {
                            System.out.println("当前教师的" + type + "课程授课信息删除失败！");
                        }
                    }
                }
            } else if (type.equalsIgnoreCase("edit")) {
                // 打印授课记录表
                list = teachCourseDao.query();
                print(list);

                System.out.println("------- 教师信息表 -------");
                List<Teacher> list_teacher = new TeacherDao(connection).query();
                new TeacherServiceImpl().print(list_teacher);

                System.out.println("--------- 课程信息表 ---------");
                List<Course> list_course = new CourseDao(connection).query();
                new CourseServiceImpl().print(list_course);

                System.out.println("请依次单独输入要更新授课信息的id、教师工号、课程号：");
                TeachCourse teachCourse = new TeachCourse();
                teachCourse.setTcid(scanner.nextInt());
                teachCourse.setTid(scanner.nextInt());
                teachCourse.setCid(scanner.next());
                if (teachCourseDao.update(teachCourse)) {
                    System.out.println("更新成功");
                    list = teachCourseDao.query();
                    print(list);
                } else {
                    System.out.println("更新失败");
                }
            } else if (type.equalsIgnoreCase("query")) {
                System.out.println("查询指定教师请输入教师工号，查询所有教师输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    list = teachCourseDao.query();
                    print(list);
                } else {
                    list = teachCourseDao.query(Integer.parseInt(type));
                    print(list);
                }
            } else if (type.equalsIgnoreCase("QUIT")) {
                teachCourseDao.close();
                break;
            }
        }

    }

    @Override
    public void print(List list) {

        if(list.size()>0) {

            Iterator<TeachCourse> iterator = list.iterator();
            System.out.println("id\t教师工号\t教师名\t课程号\t课程名");
            while (iterator.hasNext()) {
                TeachCourse teachCourse = iterator.next();
                String id = String.format("%-4s", teachCourse.getTcid());
                String tid = String.format("%-8s", teachCourse.getTid());
                String tname = String.format("%-6s", teachCourse.getTname());
                String cid = String.format("%-8s", teachCourse.getCid());
                String cname = String.format("%-10s", teachCourse.getCname());
                System.out.println(id + tid + tname + cid + cname);
            }
        }else{
            System.out.println("无授课记录，请录入！");
        }
    }
}
